Page({
  data: {
    avatar: '',
    nickName: ''
  },
  goLogin() {
    wx.navigateTo({
      url: '/pages/login/index',
    })
  },
  onLoad() {
    // 从app.js中拿到初始值
    // console.log(getApp().userInfo)
    const app = getApp()
    const { avatar, nickName } = app.userInfo
    this.setData({ avatar, nickName })

    // 如果有token，就发一次请求，拿到个人信息
    if (app.getToken()) {
      this.getProfile()
    }
  },
  async getProfile() {
    const { avatar, nickName } = await wx.$http.get('/userInfo')
    // 更新当前页面数据
    this.setData({ avatar, nickName })

    // 更新下app中的数据
    const app = getApp()
    app.userInfo = { avatar, nickName }
  }
})
